package cn.b.sky.user.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Collection;

/**
 * 
 * <strong>Title : ProxyServletListener.java<br></strong>
 * <strong>Package : cn.b.sky.user.filter<br></strong>
 * <strong>Description : 全局监听器监听全部的ServletContextListener和HttpSessionListener<br> </strong>
 * <strong>Description : 新增监听器就在spring配置一个bean就好<br> </strong>
 * <strong>Create on : 2016-7-11 下午7:43:51<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class ProxyServletListener implements ServletContextListener, HttpSessionListener {

  private static final Logger log = LoggerFactory.getLogger(ProxyServletListener.class);

  private ApplicationContext ctx;

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    this.ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

    if (this.ctx == null) {
      log.warn("cannot find applicationContext");

      return;
    }

    Collection<ServletContextListener> servletContextListeners = this.ctx.getBeansOfType(ServletContextListener.class)
        .values();

    for (ServletContextListener servletContextListener : servletContextListeners) {
      servletContextListener.contextInitialized(sce);
    }
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    if (this.ctx == null) {
      log.warn("cannot find applicationContext");

      return;
    }

    Collection<ServletContextListener> servletContextListeners = this.ctx.getBeansOfType(ServletContextListener.class)
        .values();

    for (ServletContextListener servletContextListener : servletContextListeners) {
      servletContextListener.contextDestroyed(sce);
    }
  }

  @Override
  public void sessionCreated(HttpSessionEvent se) {
    if (this.ctx == null) {
      log.warn("cannot find applicationContext");

      return;
    }

    Collection<HttpSessionListener> httpSessionListeners = this.ctx.getBeansOfType(HttpSessionListener.class).values();

    for (HttpSessionListener httpSessionListener : httpSessionListeners) {
      httpSessionListener.sessionCreated(se);
    }
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent se) {
    if (this.ctx == null) {
      log.warn("cannot find applicationContext");

      return;
    }

    Collection<HttpSessionListener> httpSessionListeners = this.ctx.getBeansOfType(HttpSessionListener.class).values();

    for (HttpSessionListener httpSessionListener : httpSessionListeners) {
      httpSessionListener.sessionDestroyed(se);
    }
  }
}
